/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;

import entities.Car;
import hibernateUtil.HibernateSessionFactory;
import java.util.ArrayList;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Session;
import org.hibernate.Transaction;

public class CarDao {
    private static final Log log = LogFactory.getLog(CarDao.class);
    
    public void save(Car car){
        Session session = null;
        Transaction transaction = null;
        log.debug("persisting CarDao instance");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            session.save(car);
            log.debug("save successful");
	} catch (RuntimeException re) {
            log.error("save failed", re);
            throw re;
	}
        finally{
            transaction.commit();
            session.close();
        }        
    }
    
    public Car findById(int id){
        Session session = null;
        Transaction transaction = null;
        log.debug("getting Car instance with id: " + id);
        try{
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            Car instance = (Car) session.get(Car.class, id);
            if (instance == null) {
                log.debug("get successful, no instance found");
            } else {
		log.debug("get successful, instance found");
            }
		return instance;
        } catch (RuntimeException re){
            	log.error("get failed", re);
		throw re;
        } finally {
            transaction.commit();
            session.close();
        }
    }    
    
    public ArrayList<Car> getAllCars(){
        Session session = null;
        Transaction transaction = null;
        ArrayList<Car> carList = null;
        log.debug("getting Cars List");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            carList = (ArrayList<Car>) session.createCriteria(Car.class).list();
            if(carList.isEmpty()){
                log.debug("get successful, empty carList");
            } else {
                log.debug("get successful, not empty carList");
            }
        } catch (RuntimeException re) {
              log.error("get failed", re);
              throw re;
        } finally {
            transaction.commit();
            session.close();
        }
        return carList;
    }    
    
    public void update(Car car){
        Session session = null;
        Transaction transaction = null;
        log.debug("update Car");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            session.update(car);
            log.debug("update successful");
	} catch (RuntimeException re) {
            log.error("update failed", re);
            throw re;
	}
        finally{
            transaction.commit();
            session.close();
        }        
    }
    
    public void delete(Car car){
        Session session = null;
        Transaction transaction = null;
        log.debug("delete Car");
        try {
            session = HibernateSessionFactory.getSessionFactory().openSession();
            transaction = session.beginTransaction();
            session.delete(car);
            log.debug("delete car successful");
	} catch (RuntimeException re) {
            log.error("delete car failed", re);
            throw re;
	}
        finally{
            transaction.commit();
            session.close();
        }        
    }     
        
}
